package com.gexiaobao.pigeon.viewmodel

import androidx.lifecycle.MutableLiveData
import com.gexiaobao.pigeon.app.model.bean.AddressListResponse
import com.gexiaobao.pigeon.app.model.bean.FeatherColorResponse
import com.gexiaobao.pigeon.app.model.bean.ProvinceListResponse
import com.gexiaobao.pigeon.app.model.bean.UiState
import com.gexiaobao.pigeon.app.model.bean.VersionInfoResponse
import com.gexiaobao.pigeon.app.model.param.DeviceIdParams
import com.gexiaobao.pigeon.app.model.param.OutChatParam
import com.gexiaobao.pigeon.app.network.ListDataUiState
import com.gexiaobao.pigeon.app.network.apiService
import com.gexiaobao.pigeon.app.util.RxToast
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request
import me.hgj.jetpackmvvm.state.ResultState

/**
 * created by : huxiaowei
 * @date : 20220920
 * Describe :
 */
class MainViewModel : BaseViewModel() {

    /** 获取省份列表 */
    val provinceListResult: MutableLiveData<ProvinceListResponse> = MutableLiveData()
    fun getProvinceList() {
        request({ apiService.getProvinceList() }, {
            provinceListResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }

    /** 删除收货地址 */
    val delAddressResult: MutableLiveData<UiState> = MutableLiveData()
    fun delShippingAddress(id: String) {
        request({ apiService.delShippingAddress(id) }, {
            val deleteState = UiState(isSuccess = true)
            delAddressResult.value = deleteState
        }, {
            val deleteState = UiState(isSuccess = false, errorMsg = it.msg)
            delAddressResult.value = deleteState
        })
    }

    //羽色列表
    var featherColor: MutableLiveData<ListDataUiState<FeatherColorResponse.ColorList>> = MutableLiveData()

    /** 获取羽色列表 */
    fun getFeatherColor() {
        request({ apiService.getFeatherColor() }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true, isEmpty = it.isEmpty(), listData = it.list
            )
            featherColor.value = listDataUiState
        }, {
            val listDataUiState = ListDataUiState(isSuccess = false, errMessage = it.msg, listData = arrayListOf<FeatherColorResponse.ColorList>())
            featherColor.value = listDataUiState
        })
    }

    /** 获取版本信息 */
    val versionResult: MutableLiveData<ResultState<VersionInfoResponse>> = MutableLiveData()
    fun upDateVersion() {
        request(
            { apiService.upDateVersion() },
            versionResult,
        )
    }

    /** 上传deviceId */
    val deviceIdResult: MutableLiveData<UiState> = MutableLiveData()
    fun updateDeviceId(body: DeviceIdParams) {
        request(
            { apiService.updateDeviceId(body) }, {
                val result = UiState(isSuccess = true)
                deviceIdResult.value = result
            }, {
                val result = UiState(isSuccess = false, errorMsg = it.msg)
                deviceIdResult.value = result
            }
        )
    }

    /** 获取用户设置地址  */
    val addressResult: MutableLiveData<ListDataUiState<AddressListResponse.ListBean>> = MutableLiveData()
    fun getAddressList() {
        request({ apiService.getShippingAddress() }, {
            val listDataUiState = ListDataUiState(
                isSuccess = true,
                isRefresh = true,
                isEmpty = it.isEmpty(),
                total = it.total,
                hasMore = it.hasMore(),
                isFirstEmpty = it.isEmpty(),
                listData = it.list
            )
            addressResult.value = listDataUiState
        }, {
            val listDataUiState =
                ListDataUiState(
                    isSuccess = false,
                    isRefresh = false,
                    errCode = it.code,
                    errMessage = it.msg,
                    listData = arrayListOf<AddressListResponse.ListBean>()
                )
            addressResult.value = listDataUiState
        })
    }

    /** 退出对话 */
    val outChatResult: MutableLiveData<Any> = MutableLiveData()
    fun outChat(body: OutChatParam) {
        request({ apiService.outChat(body) }, {
            outChatResult.value = it
        }, {
            RxToast.showToast(it.msg)
        })
    }
}